package com.eastfair.pay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.pay.dao.InvoiceTitleMapper;
import com.eastfair.pay.dto.InvoiceTitleDTO;
import com.eastfair.pay.entity.InvoiceTitle;
import com.eastfair.pay.service.InvoiceTitleService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 发票抬头
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Service
public class InvoiceTitleServiceImpl extends SuperServiceImpl<InvoiceTitleMapper, InvoiceTitle> implements InvoiceTitleService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<InvoiceTitle> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(InvoiceTitle model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        return R.successDef();
    }

    @Override
    public void saveOrUpdate(InvoiceTitleDTO invoiceTitleDTO) {

        InvoiceTitle invoiceTitle = BeanUtil.copyProperties(invoiceTitleDTO, InvoiceTitle.class);
        LambdaQueryWrapper<InvoiceTitle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvoiceTitle::getInvoiceTitle, invoiceTitleDTO.getInvoiceTitle())
                .eq(InvoiceTitle::getInvoiceDutyParagraph, invoiceTitleDTO.getInvoiceDutyParagraph())
                .last("limit 1");

        InvoiceTitle invoiceTitleQuery = baseMapper.selectOne(lambdaQueryWrapper);
        if (invoiceTitleQuery != null){
            BeanUtil.copyProperties(invoiceTitle, invoiceTitleQuery);
            updateById(invoiceTitleQuery);
        } else {
            save(invoiceTitle);
        }

    }

    @Override
    public List<InvoiceTitle> list(InvoiceTitleDTO invoiceTitleDTO) {
        LambdaQueryWrapper<InvoiceTitle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StrUtil.isBlank(invoiceTitleDTO.getInvoiceTitle()), InvoiceTitle::getInvoiceTitle, invoiceTitleDTO.getInvoiceTitle())
                .eq(!StrUtil.isBlank(invoiceTitleDTO.getInvoiceDutyParagraph()), InvoiceTitle::getInvoiceDutyParagraph, invoiceTitleDTO.getInvoiceDutyParagraph());
        List<InvoiceTitle> invoiceTitles = baseMapper.selectList(lambdaQueryWrapper);
        return invoiceTitles;
    }
}
